package com.xw.userService.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JsonUtils {

	public static Gson getGson() {
		Gson gson = new Gson();
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.registerTypeAdapter(LocalDate.class, new LocalDateSerializer());
		gsonBuilder.registerTypeAdapter(LocalDate.class, new LocalDateJsonDeserializer());
		gsonBuilder.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer());
		gsonBuilder.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonDeserializer());
		gson = gsonBuilder.create();
		return gson;
	}

	public static String toJson(Object object) {
		return getGson().toJson(object);
	}

	public static <T> T toBean(String jsonStr, Class<T> beanClass) {
		return getGson().fromJson(jsonStr, beanClass);
	}
}
